include ../config.mk
CXXFLAGS+=$(CPPFLAGS) -std=c++0x -DNDEBUG -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer
ifeq ($(NEED_GPERFTOOLS), 1)
	CXXFLAGS+=-DBRPC_ENABLE_CPU_PROFILER
endif

# 基本配置信息
NEED_GPERFTOOLS=0
ROOT_PATH=../../..
XML_DIR=$(ROOT_PATH)/xunmenglong
BRPC_PATH=$(ROOT_PATH)/third_party/BRPC
THIRD_PARTY_DIR=$(ROOT_PATH)/third_party

# 引用依赖库
## 添加xproto
HDRS+=../output
LIBS+=../output
STATIC_LINKINGS+=-lxproto

## BRPC
HDRS+=$(BRPC_PATH)/output/include
LIBS+=$(BRPC_PATH)/output/lib
STATIC_LINKINGS+=-lbrpc

## util库
HDRS+=$(XML_DIR)/xutil/

## third_pary相关
XXHASH_PATH=$(THIRD_PARTY_DIR)/xxHash/
HDRS+=$(XXHASH_PATH)
LIBS+=$(XXHASH_PATH)
STATIC_LINKINGS+=-lxxhash

##### 引用依赖库结束 ######

HDRPATHS=$(addprefix -I, $(HDRS))
LIBPATHS=$(addprefix -L, $(LIBS))

# 配置protobuf相关操作
PROTOS = $(wildcard *.proto)
PROTO_OBJS = $(PROTOS:.proto=.pb.o)
PROTO_GENS = $(PROTOS:.proto=.pb.h) $(PROTOS:.proto=.pb.cc)

# 链接相关配置
LINK_OPTIONS = -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS)

ALL_CPP_FILE=$(wildcard *.cpp)
ALL_EXE_FILE=$(basename $(ALL_CPP_FILE))

.PHONY:all
all: $(ALL_EXE_FILE)

.PHONY:clean
clean:
	@echo "> Cleaning"
	rm -rf *.o $(ALL_EXE_FILE)

%.pb.cc %.pb.h:%.proto
	@echo "> Generating $@"
	$(PROTOC) --cpp_out=. --proto_path=. $(PROTOC_EXTRA_ARGS) $<

%.o:%.cc
	@echo "> Compiling $@"
	$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@

%.o:%.cpp
	@echo "> Compiling $@"
	@echo "> testing $(HDRPATHS)"
	$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@

$(ALL_EXE_FILE) : % : %.o $(PROTO_OBJS)
	@echo "> Linking $@"
	$(CXX) $(LIBPATHS) $(LINK_OPTIONS) -o $@
	rm $^
